package com.binya.lib.permission.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.binya.lib.permission.R;

import java.util.List;

/**
 * @author ydh
 * Time 2023年06月27日
 */
public class PermissionDesView extends LinearLayout {

    public PermissionDesView(Context context) {
        this(context, null);
    }

    public PermissionDesView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PermissionDesView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        setOrientation(VERTICAL);
    }

    public void showData(List<PermissionDesBean> list) {
        removeAllViews();
        if (list == null || list.size() < 1) {
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            PermissionDesBean bean = list.get(i);
            addItem(bean);
            if (i < list.size() - 1) {
                TextView textView = new TextView(getContext());
                textView.setHeight(dip2px(getContext(), 20));
                addView(textView);
            }
        }
    }

    private void addItem(PermissionDesBean bean) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.item_permission_des, null);

        TextView tv_title = view.findViewById(R.id.tv_title);
        TextView tv_des = view.findViewById(R.id.tv_des);

        tv_title.setText(bean.getTitle());
        tv_des.setText(bean.getDes());

        addView(view);
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

}
